/*     */ package com.slim.service.serviceimpl;
/*     */ 
/*     */ import java.io.Serializable;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import org.apache.commons.logging.Log;
/*     */ import org.apache.commons.logging.LogFactory;
/*     */ import org.appfuse.dao.GenericDao;
/*     */ import com.slim.service.GenericManager;
/*     */ import org.compass.core.CompassHit;
/*     */ import org.compass.core.support.search.CompassSearchCommand;
/*     */ import org.compass.core.support.search.CompassSearchHelper;
/*     */ import org.compass.core.support.search.CompassSearchResults;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ 
/*     */ public class GenericManagerImpl<T, PK extends Serializable>
/*     */   implements GenericManager<T, PK>
/*     */ {
/*  55 */   protected final Log log = LogFactory.getLog(getClass());
/*     */   protected GenericDao<T, PK> dao;
/*     */ 
/*     */   @Autowired
/*     */   private CompassSearchHelper compass;
/*     */ 
/*     */   public GenericManagerImpl()
/*     */   {
/*     */   }
/*     */ 
/*     */   public GenericManagerImpl(GenericDao<T, PK> genericDao)
/*     */   {
/*  69 */     this.dao = genericDao;
/*     */   }
/*     */ 
/*     */   public List<T> getAll()
/*     */   {
/*  76 */     return this.dao.getAll();
/*     */   }
/*     */ 
/*     */   public T get(PK id)
/*     */   {
/*  83 */     return this.dao.get(id);
/*     */   }
/*     */ 
/*     */   public boolean exists(PK id)
/*     */   {
/*  90 */     return this.dao.exists(id);
/*     */   }
/*     */ 
/*     */   public T save(T object)
/*     */   {
/*  97 */     return this.dao.save(object);
/*     */   }
/*     */ 
/*     */   public void remove(PK id)
/*     */   {
/* 104 */     this.dao.remove(id);
/*     */   }
/*     */ 
/*     */   public List<T> search(String q, Class clazz)
/*     */   {
/* 114 */     if ((q == null) || ("".equals(q.trim()))) {
/* 115 */       return getAll();
/*     */     }
/*     */ 
/* 118 */     List results = new ArrayList();
/*     */ 
/* 120 */     CompassSearchCommand command = new CompassSearchCommand(q);
/* 121 */     CompassSearchResults compassResults = this.compass.search(command);
/* 122 */     CompassHit[] hits = compassResults.getHits();
/*     */ 
/* 124 */     if ((this.log.isDebugEnabled()) && (clazz != null)) {
/* 125 */       this.log.debug("Filtering by type: " + clazz.getName());
/*     */     }
/*     */ 
/* 128 */     for (CompassHit hit : hits) {
/* 129 */       if (clazz != null) {
/* 130 */         if (hit.data().getClass().equals(clazz))
/* 131 */           results.add(hit.data());
/*     */       }
/*     */       else {
/* 134 */         results.add(hit.data());
/*     */       }
/*     */     }
/*     */ 
/* 138 */     if (this.log.isDebugEnabled()) {
/* 139 */       this.log.debug("Number of results for '" + q + "': " + results.size());
/*     */     }
/*     */ 
/* 142 */     return results;
/*     */   }
/*     */ }

